package com.liuyu.common.framework.common.utils;

/**
 * @ClassName Base64
 * @Author: liuyu
 * @Date: Created in 2021/6/10 16:27
 * @Version: 1
 * @Modified By:
 */

import cn.hutool.core.codec.Base64Decoder;
import cn.hutool.core.codec.Base64Encoder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.*;

@Slf4j
public class Base64 {

    /**
     *
     *将图片文件转化为字节数组字符串，并对其进行Base64编码处理
     * @author liuyu
     * @date 2021/8/23 9:22
     * @param imgFile
     * @return java.lang.String
     */
    public static String getImageStr(String imgFile) {
        byte[] data = null;
        //读取图片字节数组
        try (InputStream in = new FileInputStream(imgFile)) {
            data = new byte[in.available()];
            int count = 0;
            while ((count = in.read(data)) > 0) {

            }
        } catch (IOException e) {
            log.info(e.getMessage(), e);
        }
        //对字节数组Base64编码
//        return Base64Encoder.encode(data);//返回Base64编码过的字节数组字符串
        //返回Base64编码过的字节数组字符串
        return cn.hutool.core.codec.Base64.encode(data);
    }


    /**
     *获取base64字符串
     */
    public static String encodeBase64(String filaName, boolean isSafe) {
        if (StringUtils.isBlank(filaName)) {
            throw new NullPointerException();
        }

        byte[] data = null;
        String encodedText = null;
        //读取图片字节数组
        try (InputStream in = new BufferedInputStream(new FileInputStream(filaName))) {
            data = new byte[in.available()];
            int count = 0;
            while ((count = in.read(data)) > 0) {

            }
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
        //对字节数组Base64编码
        if (isSafe) {
            java.util.Base64.Encoder encoder = java.util.Base64.getUrlEncoder();
            encodedText = encoder.encodeToString(data);
        } else {
            encodedText = Base64Encoder.encode(data);
            encodedText = encodedText.replaceAll("[\\s*\t\n\r]", "");
        }
        return encodedText;
    }

    /**
     *解析base64
     */
    public static String decodeBase64(String base64, String filePath, String suffix, boolean isSafe) {
        if (StringUtils.isBlank(base64) || StringUtils.isBlank(filePath) || StringUtils.isBlank(suffix)) {
            throw new NullPointerException();
        }
        String fileName = null;
        File file = new File(filePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        fileName = filePath + System.currentTimeMillis() + "." + suffix;
        try (OutputStream out = new BufferedOutputStream(new FileOutputStream(fileName))) {
            byte[] b = new byte[2048];
            if (isSafe) {
                java.util.Base64.Decoder decoder = java.util.Base64.getUrlDecoder();
                b = decoder.decode(base64);
            } else {
                b = Base64Decoder.decode(base64.substring(base64.indexOf(",") + 1));
            }
            for (int i = 0; i < b.length; ++i) {
                // 调整异常数据
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }

            out.write(b);
            out.flush();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return fileName;
    }

    public static void main(String[] args) {
        log.info(encodeBase64("K:\\gogs\\iinsure\\iinsure-master\\start\\src\\main\\resources\\image\\image001.jpg", true));
        log.info(getImageStr("K:\\gogs\\iinsure\\iinsure-master\\start\\src\\main\\resources\\image\\image001.jpg"));
    }
}
